package handler

import (
	"git.oschina.net/fanbuchi/redpack/constant"
	"git.oschina.net/fanbuchi/redpack/domain"
	_error "git.oschina.net/fanbuchi/redpack/errors"
	"git.oschina.net/fanbuchi/redpack/service"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"net/http"
	"strconv"
)


type WalletHandler struct {
	service.WalletService
}

func init() {

}


// @Summary 创建钱包
// @Description 创建钱包
// @Tags wallet
// @Accept  json
// @Produce  json
// @Param   wallet     body    domain.Wallet     true        "创建钱包"
// @Param   Authorization     header    string     true      "权限认证" default(Bearer 3bbea1b6-f8e4-4da9-a9f3-0413abff47c5)
// @Success 200 {object} domain.Wallet    "调用成功"
// @Failure 400 {object} domain.Rsp "失败，参数错误!"
// @Router /wallet/create [post]
func  (handler*WalletHandler)Create(c *gin.Context)  {
	//binding
	var params domain.Wallet
	if err := c.ShouldBindBodyWith(&params, binding.JSON); err != nil {
		c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusBadRequest,Msg:err.Error()})
		return
	}
	//srv
	accountId := c.MustGet(constant.AccountId)
	_accountId:=accountId.(int64)
	params.AccountId=_accountId

	out,err:=handler.WalletService.Create(&params)
	if err != nil {
		c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusBadRequest,Msg:err.Error()})
		return
	}
	//end
	c.JSON(http.StatusOK,domain.Rsp{Status:http.StatusOK,Data:out})
}





// @Summary 删除钱包
// @Description 删除钱包
// @Tags wallet
// @Accept  json
// @Produce  json
// @Param    id    query    int     true        "id" default(1)
// @Param   Authorization     header    string     true        "权限认证" default(Bearer 3bbea1b6-f8e4-4da9-a9f3-0413abff47c5)
// @Success 200 {object} domain.Account    "调用成功"
// @Failure 400 {object} domain.Rsp "失败，参数错误!"
// @Router /wallet/delete [delete]
func  (handler*WalletHandler)Delete(c *gin.Context)  {
	var params domain.Wallet
	if err := c.ShouldBindQuery(&params);err!=nil {
		c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusBadRequest,Msg:err.Error()})
		return
	}
	accountId := c.MustGet(constant.AccountId)
	_accountId:=accountId.(int64)
	accountRole:=c.GetString(constant.AccountRole)
	//普通用戶不能删除其他人的信息
	if accountRole!=domain.ROLE_ADMIN{
		params.AccountId=_accountId
	}
	_,err:=handler.WalletService.Delete(&params)
	if err!=nil {
		c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusForbidden,Msg:err.Error()})
		return
	}
	c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusOK,Msg:"删除成功"})
}





// @Summary 更新钱包
// @Description 更新钱包
// @Tags wallet
// @Accept  json
// @Produce  json
// @Param   wallet     body    domain.Wallet     true        "更新钱包"
// @Param   Authorization     header    string     true        "权限认证" default(Bearer 3bbea1b6-f8e4-4da9-a9f3-0413abff47c5)
// @Success 200 {object} domain.Account    "调用成功"
// @Router /wallet/update [put]
func  (handler*WalletHandler)Update(c *gin.Context)  {
	var params domain.Wallet
	if err := c.ShouldBindJSON(&params);err!=nil {
		c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusBadRequest,Msg:err.Error()})
		return
	}
	accountId := c.MustGet(constant.AccountId)
	_accountId:=accountId.(int64)
	accountRole:=c.GetString(constant.AccountRole)
	//普通用戶不能更新其他人的信息
	if accountRole!=domain.ROLE_ADMIN{
		params.AccountId=_accountId
	}
	params.DeletedAt=nil
	_,err:=handler.WalletService.Update(&params)
	if err!=nil {
		c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusForbidden,Msg:err.Error()})
		return
	}
	c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusOK,Msg:"更新成功"})
}






// @Summary 查询钱包余额
// @Description QueryByAccountId 查询钱包余额
// @Tags wallet
// @Accept  json
// @Produce  json
// @Param   Authorization     header    string     true        "权限认证" default(Bearer 3bbea1b6-f8e4-4da9-a9f3-0413abff47c5)
// @Success 200 {object} domain.Wallet    "调用成功"
// @Router /profile/queryByAccountId [get]
func  (handler*WalletHandler)QueryByAccountId(c *gin.Context)  {
	accountId:=c.GetInt64(constant.AccountId)
	p:=domain.Wallet{AccountId:accountId}
	result,err:=handler.WalletService.QueryByParam(&p)
	if err!=nil {
		c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusForbidden,Msg:err.Error()})
		return
	}

	accountRole:=c.GetString(constant.AccountRole)
	//普通用戶不能查詢其他人的信息
	if (accountRole==domain.ROLE_USER || accountRole==domain.ROLE_SUPER_USER) &&
		result.ID!=accountId{
		c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusForbidden,Msg:_error.ErrAccountOnlyYou.Error()})
		return
	}
	c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusOK,Data:result})
}


// @Summary 查询钱包列表
// @Description search 查询钱包列表
// @Tags wallet
// @Accept  json
// @Produce  json
// @Param   wallet     body    domain.Wallet     true        "查询结构条件"
// @Param   page     query    int     true        "page" default(1)
// @Param   size     query    int     true        "size" default(2)
// @Param   Authorization     header    string     true        "权限认证" default(Bearer 3bbea1b6-f8e4-4da9-a9f3-0413abff47c5)
// @Success 200 {object} domain.Wallet    "调用成功"
// @Router /wallet/search [post]
func  (handler*WalletHandler)Search(c *gin.Context)  {
	page:= c.DefaultQuery("page","1")
	size:= c.DefaultQuery("size","18")
	var req domain.Wallet
	err := c.ShouldBindBodyWith(&req,binding.JSON)
	if err!=nil {
		c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusBadRequest,Msg:err.Error()})
		return
	}

	_page,_:=strconv.Atoi(page)
	_size,_:=strconv.Atoi(size)
	ret,err:=handler.WalletService.Search(&req,_page,_size)
	if err!=nil {
		c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusBadRequest,Msg:err.Error()})
		return
	}
	c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusOK,Data:ret})
}
